<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>组件要注意的细节</title>
  <script src="./vue.js"></script>
</head>

<body>
  <div id="root">
    <counter @change="handleChange" ref="one"></counter>
    <counter @change="handleChange" ref="two"></counter>
    <div>{{totle}}</div>
  </div>
  <script>
    // totle 对两个子组件数据进行求和
    Vue.component("counter", {
      template: "<div @click='handleClick'>{{num}}</div>",
      data: function() {
        return {
          num: 0
        }
      },
      methods: {
        handleClick: function() {
          this.num ++;
          this.$emit('change'); // 组件数据发生改变，对外发布消息
        }
      }
    })
    var vm = new Vue({
      el: "#root",
      data: {
        totle: 0
      },
      methods: {
        handleChange: function() {
          // console.log("change");
          this.totle = this.$refs.one.num + this.$refs.two.num;
        }
      }
    })
    

  </script>
</body>

</html>